package cn.tedu.tea.infrastructure.data.persistence;

import cn.tedu.tea.dao.api.dao.IContentDao;
import cn.tedu.tea.dao.api.dao.IContentDetailDao;
import cn.tedu.tea.dao.api.po.Content;
import cn.tedu.tea.domain.bo.ContentBO;
import cn.tedu.tea.domain.repository.ContentRepository;
import cn.tedu.tea.infrastructure.data.converter.ContentConterver;
import cn.tedu.tea.protocol.param.ContentParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ContentRepositoryImpl implements ContentRepository {
    @Autowired
    private IContentDao contentDAO;
    @Autowired
    private IContentDetailDao contentDetailDao;
    @Autowired
    private ContentConterver contentConterver;

    @Override
    public int insert(ContentParam contentParam) {
        Content content =contentConterver.paramToPo(contentParam);
        return contentDAO.insert(content);
    }

    @Override
    public int updateByPrimaryKey(ContentParam contentParam) {
        Content content = contentConterver.paramToPo(contentParam);
        return contentDAO.update(content);
    }

    @Override
    public int delete(int id) {
        contentDetailDao.deleteById(id);
        return contentDAO.deleteById(id);
    }

    @Override
    public List<ContentBO> selectAll() {
        List<Content> list = contentDAO.selectAll();
        list.forEach(content -> System.out.println(content));
        List<ContentBO> contentBOList = contentConterver.poListToBOList(list);
        contentBOList.forEach(bo-> System.out.println(bo));
        return contentBOList;
    }

    @Override
    public ContentBO selectById(int id) {
        ContentBO contentBO = contentConterver.poToBo(contentDAO.selectByPrimaryKey(id));
        return contentBO;
    }
}
